package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.BangbiBean;
import com.wude.guild.adapter.BangBiAdapter;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("HandlerLeak")
public class BangBiActivity extends TitleBaseActivity {

    @BindView(R.id.bangbi_listview)
    ListView bangbiListview;
    @BindView(R.id.bangbi_springview)
    SpringView bangbiSpringview;
    private int limit = 1;
    List<BangbiBean> banglist = new ArrayList<>();
    private BangBiAdapter bangBiAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bang_bi);
        ButterKnife.bind(this);
        setHeaderTitle("绑定快乐币余额");
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initData() {
        bangBiAdapter = new BangBiAdapter(this);
        bangbiListview.setAdapter(bangBiAdapter);
        bangbiSpringview.setType(SpringView.Type.FOLLOW);
        bangbiSpringview.setListener(onFreshListener);
        bangbiSpringview.setHeader(new SimpleHeader(this));
        bangbiSpringview.setFooter(new SimpleFooter(this));
        initAndReflsh();
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh();
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
        }
    };


    /**
     * 刷新
     */
    private void initAndReflsh() {
        limit = 1;
        banglist.clear();
        Map<String, String> map = new HashMap<String, String>();
        map.put("p", limit + "");
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(handler, HttpCom.getBBJLURL(), map, false);
    }

    /**
     * 加载更多
     */
    private void onLoadMord() {
        limit = ++limit;
        Map<String, String> map = new HashMap<String, String>();
        map.put("p", limit + "");
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(mHandler, HttpCom.getBBJLURL(), map, false);
    }

    /**
     * 刚进来和刷新的Handler
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            bangbiSpringview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<BangbiBean> dnsbbjl = HttpUtils.DNSBBJL(msg.obj.toString());
                    if (dnsbbjl != null) {
                        banglist.addAll(dnsbbjl);
                        bangBiAdapter.setList(banglist);
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
                default:
                    break;
            }
        }
    };


    /**
     * 加载更多的Handler
     */
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            bangbiSpringview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    List<BangbiBean> dnsbbjl = HttpUtils.DNSBBJL(msg.obj.toString());
                    if (dnsbbjl != null) {
                        banglist.addAll(dnsbbjl);
                        bangBiAdapter.setList(banglist);
                    } else {
                        ToastUtil.showToast("已经到底了");
                    }
                    break;
                case 2:

                    break;
                default:
                    break;
            }
        }
    };
}
